# JavaScript var Statement

JavaScript Statements Reference

## Example

Create a variable called carName and assign the value "Volvo" to it:

var carName = "Volvo";
Try it yourself »

More "Try it Yourself" examples below.

## Definition and Usage

The var statement declares a variable.

Variables are containers for storing information.

Creating a variable in JavaScript is called "declaring" a variable:

var carName;

After the declaration, the variable is empty (it has no value).

To assign a value to the variable, use the equal sign:

carName = "Volvo";

You can also assign a value to the variable when you declare it:

var carName = "Volvo";

For more information about variables, read our JavaScript Variables Tutorial and JavaScript Scope Tutorial.

## Browser Support

Statement |  |  |  |  |
--- | --- | --- | --- | --- | ---
var | Yes | Yes | Yes | Yes | Yes

## Syntax

var varname = value;

## Parameter Values

Parameter | Description
--- | ---

varname

Required. Specifies the name of the variable.

Variable names can contain letters, digits, underscores, and dollar signs.

- Variable names must begin with a letter
- Variable names can also begin with $ and _
- Variable names are case sensitive (y and Y are different variables)
- Reserved words (like JavaScript keywords) cannot be used as variable names

value | Optional. Specifies the value to assign to the variable. Note: A variable declared without a value will have the value undefined

## Technical Details

JavaScript Version: | 1.0

## More Examples

## Example

Create two variables. Assign the number 5 to x, and 6 to y. Then display the result of x + y:

var x = 5;
var y = 6;
document.getElementById("demo").innerHTML = x + y;
Try it yourself »

## Example

You can declare many variables in one statement.

Start the statement with var and separate the variables by comma:

var lastName = "Doe",
age = 30,
job = "carpenter";
Try it yourself »

## Example

Using variables in loops:

var text = "";
var i;
for (i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
Try it yourself »

## Related Pages

JavaScript Tutorial: JavaScript Variables

JavaScript Tutorial: JavaScript Scope

JavaScript Statements Reference

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples